package com.lngsyz.eseal.base.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import org.apache.commons.codec.binary.Base64;

public class CommonUtil {
	
	/**
     * 例如:2018-12-28
     */
    public static final String DATE = "yyyy-MM-dd";
    /**
     * 例如:2018-12-28 10:00:00
     */
    public static final String DATE_TIME = "yyyy-MM-dd HH:mm:ss";

	public static Date addHour(Date date, int hour) {
		Calendar cal = Calendar.getInstance();  
		cal.setTime(date);  
		// 24小时制  
		cal.add(Calendar.HOUR, hour); 
		return cal.getTime();
	}
	
	public static String dateToStrLong(Date dateDate) {  
		if(null == dateDate) {
			return "";
		}
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    String dateString = formatter.format(dateDate);  
	    return dateString;  
	} 
	
	/** 
	 * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss 
	 *  
	 * @param strDate 
	 * @return 
	 */  
	public static Date strToDateLong(String strDate) {
		if(StringUtils.isBlank(strDate)) {
			return null;
		}
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    formatter.setLenient(false);
	    ParsePosition pos = new ParsePosition(0);  
	    Date strtodate = formatter.parse(strDate, pos);  
	    return strtodate;  
	}
	
	public static Date strToDateShort(String strDate) {
		if(StringUtils.isBlank(strDate)) {
			return null;
		}
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
	    formatter.setLenient(false);
	    ParsePosition pos = new ParsePosition(0);  
	    Date strtodate = formatter.parse(strDate, pos);  
	    return strtodate;  
	}
	
	/**
	* 判断时间格式 格式必须为“YYYY-MM-dd” 2004-2-30 是无效的 2003-2-29 是无效的
	* 
	* @param sDate
	* @return
	*/
	public static boolean isValidDate(String str, String format) {
		// String str = "2007-01-02";
		DateFormat formatter = new SimpleDateFormat(format);
		try {
			Date date = (Date) formatter.parse(str);
			return str.equals(formatter.format(date));
		} catch (Exception e) {
			return false;
		}
	}
	
	public static String getDate(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }
	
	public static void main(String args[]) {
		//System.out.println("" + Math.ceil((double)79/(double)20));
		System.out.println(dateCompare("2022-10-18 09:34:00"));
	}
	
	/**
     * 判断当前时间是否大于某个时间
     * @param time yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static boolean dateCompare(String time, int min){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            long start = sdf.parse(time).getTime();
            //获取当前时间毫秒值
            long current = System.currentTimeMillis();// 返回一个毫秒数
            //判断当前时间大于30分钟
            if((current - start)/ 1000 / 60 > min){
                //System.out.println("大于21min---------------");
                return true;
            }
            //System.out.println("小于21min----------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    
    /**
	 * true : 时间短，没到限定时间
	 * false: 时间长，超过限定时间
	 * @param fromDate
	 * @param toDate
	 * @param limitSecond
	 * @return
	 */
	public static boolean calculateDuration(Date fromDate, Date toDate, long limitSecond) {
		try {
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
//			Date fromDate = sdf.parse("2018-03-01 12:00:00");  
//			Date toDate = sdf.parse("2018-03-01 14:00:00");  
			long from3 = fromDate.getTime();  
			long to3 = toDate.getTime();  
			long second = (int) ((to3 - from3) / (1000));
			System.out.println("两个时间之间的差为：" + second);
			System.out.println("限制时间为：" + limitSecond);
			// 如果间隔时间超过限制时间，返回false
			if(second > limitSecond) {
				return false;
			}
		} catch(Exception e) {
			return false;
		}
		return true;
	}
    
    public static boolean dateCompare(String time) {
    	return dateCompare(time, 30);
    }
    
    public static String createUuid() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
    
    public static void getMkdir(String folderPath) {
		File inbox = new File(folderPath);
		if (!inbox.exists()) {
			inbox.mkdirs();
		}
	}
    
    public static boolean saveFile(MultipartFile file, String imageFolder, String imageName)
			throws IllegalStateException, IOException {
		try {
			getMkdir(imageFolder);
			File filePath = new File(imageFolder);
			if (!filePath.exists()) {
				filePath.mkdirs();
			}
			if (file.isEmpty()) {
				return false;
			}
			File f = new File(imageFolder + "/" + imageName);
			file.transferTo(f);
		} catch(Exception e) {
			return false;
		}
		return true;
	}
    
    /**
     * 手机号验证
     * 
     * @param  str
     * @return 验证通过返回true
     */
    public static boolean isMobile(String str) { 
        Pattern p = null;
        Matcher m = null;
        boolean b = false; 
        p = Pattern.compile("^[1][3,4,5,6,7,8,9][0-9]{9}$"); // 验证手机号
        m = p.matcher(str);
        b = m.matches(); 
        return b;
    }
    
    public static String localFile2Base64(String filePath) throws IOException {
        // String filePath = "path/to/your/file"; // 指定要转换的文件路径
        String encodedData = "";
        try (FileInputStream fis = new FileInputStream(new File(filePath))) {
            byte[] bytesArray = new byte[(int) new File(filePath).length()];
            fis.read(bytesArray);
            Base64 base64Encoder = new Base64();
            encodedData = base64Encoder.encodeToString(bytesArray);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return encodedData;
    }
}
